﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chloe.Entities;
using Chloe.Application.Interfaces.Bike;
using Chloe.Application.Models.Member;
using Ace;

namespace Chloe.Application.Implements.Bike
{
    public class MemberAppService : AdminAppService, IMemberAppService
    {
        public List<Bike_Member> GetMembers(string keyword)
        {
            var q = this.DbContext.Query<Bike_Member>().FilterDeleted().WhereIfNotNullOrEmpty(keyword, a => a.Name.Contains(keyword) /*|| a.EnCode.Contains(keyword)*/);
            q = q.OrderBy(a => a.SortCode);

            return q.ToList();
        }
        public List<SimpleMemberModel> GetSimpleModels()
        {
            var q = this.DbContext.Query<Bike_Member>().FilterDeleted();
            q = q.OrderBy(a => a.SortCode);

            return q.Select(a => new SimpleMemberModel() { Id = a.Id, Name = a.Name }).ToList();
        }
        public List<Bike_Member> GetSimpleModels2()
        {
            var q = this.DbContext.Query<Bike_Member>().FilterDeleted();
            q = q.OrderBy(a => a.SortCode);
            q.Select(a => new { a.Id, a.Name });
            return q.ToList();
        }
        public PagedData<Bike_Member> GetPageData(Pagination page, string keyword)
        {
            var q = this.DbContext.Query<Bike_Member>().FilterDeleted().WhereIfNotNullOrEmpty(keyword, a =>a.Name .Contains (keyword));
            q.Select(a => new SimpleMemberModel { Id = a.Id, Name =a.Name });

            //PagedData<SimpleMemberModel> pagedData = q.TakePageData(page);
            var pagedData = q.TakePageData(page);
            return pagedData;

        }
        public void Add(AddMemberInput input)
        {
            input.Validate();
            Bike_Member entity = this.CreateEntity<Bike_Member>();

            entity.Name = input.Name;
            entity.OnlineName = input.OnlineName;
            entity.IdNumber = input.IdNumber;
            entity.Gender = input.Gender;
            entity.Birthday = input.Birthday;
            entity.MobilePhone = input.MobilePhone;
            entity.IsMember = input.IsMember;
            entity.WeChat = input.WeChat;
            entity.SortCode = input.SortCode;
            entity.Description = input.Description;

            this.DbContext.Insert(entity);
        }
        public void Update(UpdateMemberInput input)
        {
            input.Validate();

            this.DbContext.Update<Bike_Member>(a => a.Id == input.Id, a => new Bike_Member()
            {
                Name = input.Name,
                OnlineName = input.OnlineName,
                IdNumber = input.IdNumber,
                Gender = input.Gender,
                Birthday = input.Birthday,
                MobilePhone = input.MobilePhone,
                IsMember = input.IsMember,
                WeChat = input.WeChat,
                SortCode = input.SortCode,
                Description = input.Description,
            });
        }
        public void Delete(string id)
        {
            this.SoftDelete<Bike_Member>(id);
        }
    }
}
